﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCalc;
using DocumentFormat.OpenXml.Packaging; 

namespace HDI.Charts2
{
    public static class Helper
    {
        //reurns a DateTime Object from SPListItem
        public static DateTime ToDate(this object date)
        {
            DateTime newDate = default(DateTime);
            if (date.ToString().Contains(";#"))
            {
                date = date.ToString().Split('#')[1];
            }
            DateTime.TryParse(date.ToString(), out newDate);
            return newDate;
        }

        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?', ',', '!', '/', ';', '-' },
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }

        public static double wochenTag(DayOfWeek tag)
        {
            switch (tag.ToString())
            {
                case "Monday": return 0;
                case "Tuesday": return 1;
                case "Wednesday": return 2;
                case "Thursday": return 3;
                case "Friday": return 4;
                case "Saturday": return 5;
                case "Sunday": return 6;
            }
            return 0;
        }

        public static DateTime gotoDayOfWeek(this DateTime first, DayOfWeek gotoD)
        {

            return first.Subtract(TimeSpan.FromDays(wochenTag(first.DayOfWeek) - wochenTag(gotoD)));
        }




        public static String getColor(String value)
        {
            string color = "";
            if (value.Contains(".g"))
            {
                color = "' class='" + value.Remove(0, 1) + "'";
            }
            else
            {

                color = " background: " + value + ";";
            }
            return color;
        }

        public static Int64 ToUnix(this DateTime time)
        {

            TimeSpan t = (time - new DateTime(1970, 1, 1).ToLocalTime());
            return (Int64)t.TotalSeconds;
        }


        public static double calculate(string x)
        {
            try
            {
                Expression e = new Expression(x);
                e.Parameters["E"] = Math.E;
                e.Parameters["Pi"] = Math.PI;

                e.EvaluateFunction += delegate(string name, FunctionArgs args)
                {
                    if (name == "Fac")
                    {
                        int number = int.Parse(args.Parameters[0].Evaluate().ToString());
                        if (number <= 20 && number >= 0)
                            args.Result = factors64[number];
                        else
                            throw new ArgumentOutOfRangeException("Die Fakultät kann nur bis 20 berechnet werden. Momentaner Wert: " + number.ToString());
                    }
                };
                return double.Parse(e.Evaluate().ToString());
            }
            catch (Exception ex)
            {
                throw new Exception("Es gibt ein Fehler in der Formel: " + x + "; Genauer Fehler: " + ex.ToString());
            }
        }

        static Int64[] factors64 = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320,
362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 
1307674368000, 20922789888000, 355687428096000, 6402373705728000, 
121645100408832000, 2432902008176640000 };
    }
}
